/*
 * @Descripttion: 医师修改拒绝预约
 * @version: 1.0
 * @Author: 自由如风
 * @Date: 2021-05-07 12:56:55
 * @LastEditors: 自由如风
 * @LastEditTime: 2021-05-07 13:28:26
 */
const DB = require('../../lib/DB');
const { Success, Fail } = require('../../lib/Response');

module.exports = async function (ctx, next) {
    try {
        const foresee = await DB.models.foresee.findOne({
            where: { id: ctx.request.body.id }, include: [
                {
                    model: DB.models.patient,
                    as: 'patient'
                },
                {
                    model: DB.models.user,
                    as: 'doctor'
                }
            ]
        })

        if (foresee.state == '等待处理') {
            foresee.state = '预约失败';
        } else {
            throw new Error("此预约状态不能更改为预约失败")
        }

        foresee.memp = ctx.request.body.memo;
        await foresee.save()
        ctx.body = new Success(foresee.toJSON())

        await next();
    } catch (e) {
        ctx.body = new Fail(e.message)
    }
}